/*
runTime: 0.0027
fileTree:
 +---- page/comManage.js
 |  +---- cpt/LJSDefine.js
 |  +---- cpt/search.js
 |  +---- cpt/allSelect.js
 |  |  +---- mod/cPrompt.js
 |  |  |  +---- mod/modTemplate.js
 |  |  |  +---- mod/cAlert.js
 |  |  |  |  +---- mod/win.js
*/
/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  评论管理页
 * @author  wangxin | xinwang200110@sohu-inc.com
 * @version 1.0 | 2013-04-17
 * @param
 * @example
 */

/**
 * Copyright (c) 2010 - 2012, Sina Inc. All rights reserved.
 * @fileoverview Sina  合辑承载页包含epg信息部分
 * @author bowen liu | liubowen082@163.com
 * @version 1.1 | 2013-4-13
 * @example
 * 
 */
if(typeof(LJS)=='undefined'){
	var LJS = {}
}
LJS.define = (function(){
    /**
	 * @id STK.register
	 * @alias STK.register
	 * @param {String} String
	 * @param {Function} Function
	 * @return {Void}
	 */
    var define = function(ns, maker) {
        var path = ns.split('.');
        var curr = LJS;
        for (var i = 0, len = path.length; i < len; i += 1) {
            if (i == len - 1) {
                if (curr[path[i]] !== undefined) {
                    throw ns + 'has been register!!!';
                }
                curr[path[i]] = maker(LJS);
                return true;
            }
            if (curr[path[i]] === undefined) {
                curr[path[i]] = {};
            }
            curr = curr[path[i]];
        }
    };
    
    
    return define
    
    
    
})();

/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  搜索框部分交互功能(需嵌套模板使用)
 * @author  wangxin | xinwang200110@sohu-inc.com
 * @version 1.0 | 2013-04-17
 * @param
 * @example
 */

LJS.define('cpt.search',function(ljs){
	var that = {};
	that.addEvent = function(){
		
		that.input.on('focus',function(){
			if(that.input[0].value == that.input.attr('defValue')){
				that.input[0].value = '';
			}
		});
		
		that.input.on('blur',function(){
			if(that.input[0].value == ''){
				that.input[0].value = that.input.attr('defValue');
			}
		});
		
		that.delBtn.on('click',function(){
			that.input[0].value = '';
			that.input[0].focus();
		});
		that.searchBtn.on('click',function(){
			if(that.input[0].value == that.input.attr('defValue')){
				that.input[0].value = '';
			}
			$(this).parent('form').submit()
		})
	}
	that.init = function(){
		that.input = $('#searchInput');
		that.delBtn = $('#del');
		that.searchBtn = $('#searchId');
		that.addEvent();
	}
	return that;
});

/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  全选、删除功能(需嵌套模板使用)
 * @author  wangxin | xinwang200110@sohu-inc.com
 * @version 1.0 | 2013-04-17
 * @param
 * @example
 */
/**
 * Copyright (c) 2012 - 2013,  Inc. All rights reserved.
 * @fileoverview   信息层-- 对话框
 * @author  bowen liu | liubowen082@163.com
 * @version 1.0 | 2013-04-13
 * @example
 */
/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  弹层包装类
 * @author  bowen liu | liubowen082@163.com
 * @version 1.0 | 2013-04-13
 * @param
 * @example
 * modTemplate('#{city||default:天津}欢迎你',{'city':'北京'}) === '北京欢迎你';
 */
LJS.define('mod.modTemplate',function(ljs){
	
	return function(template, data){
        return template.replace(/#\{(.+?)\}/ig, function(){
            var key = arguments[1].replace(/\s/ig, '');
            var ret = arguments[0];
            var list = key.split('||');
            for (var i = 0, len = list.length; i < len; i += 1) {
                if (/^default:.*$/.test(list[i])) {
                    ret = list[i].replace(/^default:/, '');
                    break;
                }
                else 
                    if (data[list[i]] !== undefined) {
                        ret = data[list[i]];
                        break;
                    }
            }
            return ret;
        });
    };
    
});

/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  信息层
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-06-29
 * @example
 */
/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu  弹出层
 * @author  bowen liu | liubowen082@163.com
 * @version 1.0 | 2013-04-13
 * @example
 */
LJS.define('mod.win',function(ljs){
    function stopBubble(e){
        if (e && e.stopPropagation) 
            e.stopPropagation()
        else 
            window.event.cancelBubble = true
    }
    var setXYCenter = function(obj, x, y){
		if (!obj) 
			return;
		x = x || 0;
		y = y || 0;
		obj.style.position = "absolute";
		obj.style.left = document.documentElement.offsetWidth / 2 - obj.offsetWidth / 2 + x + "px";
		var _scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		var top = (Math.min(document.documentElement.clientHeight,$(document).height()) - obj.offsetHeight) * 0.5 + _scrollTop + y;
		top = top < 0 ? 0 : top;
		obj.style.top = top + "px";
		return obj;
	}
    var backbg = function(){
    }
    backbg.prototype = {
        show: function(){
            if (this.div) {
                this.div.show();
                return;
            }
            this.div = document.createElement('div');
            
            $(document.body).append(this.div);
            $(this.div).css({
                top: 0,
                left: 0,
                width: Math.max($(document.body).width(), 996),
                height: $(document.body).height(),
                position: 'absolute',
                'z-index': 998,
                background: '#000',
                opacity: 0.7
            });
			
            var t = this;
            $(window).on('resize', function(){
                $(t.div).css('width', Math.max($(document.body).width(), 996) + 'px');
                
            })
        },
        del: function(){
            if (this.div) {
                $(this.div).remove();
                return;
            }
        },
        hide: function(){
            if (this.div) {
                $(this.div).hide();
                return;
            }
        }
        
    }
    
    var win = function(op){
        this.tpl = op && op.tpl;
        this.showBg = op.showBg || false;
        if (this.showBg) 
            this.bg = new backbg();
        else {
            this.bg = {
                show: function(){
                },
                del: function(){
                },
                hide: function(){
                }
            }
        }
    }
    win.prototype = {
        show: function(op){
            this.tpl = this.tpl || op && op.tpl;
            if (this.win) {
                $(this.win).show();
                this.bg.show();
                $(this).trigger('onShow');
                return
            }
            var div = document.createElement('div');
            this.bg.show();
            $(div).html(this.tpl);
            $(document.body).append(div);
            $(div).css({
                // display: 'block',
                top: '50%',
                'z-index': 999,
                position: 'absolute',
                left: '50%'
            }).addClass('popBox clearfix');
            setXYCenter(div)
            
            $(window).scroll(function(){
                setXYCenter(div)
            });
            $(window).on('resize',function(){
                setXYCenter(div)
            })
            
            this.win = div;
            this.addEvent()
            $(this).trigger('onShow');
        },
        close: function(){
            if (this.win) {
                $(this.win).hide();
                this.bg.hide();
                $(this).trigger('onClose')
            }
        },
        del: function(){
            if (this.win) {
                $(this.win).remove();
                this.bg.del();
                $(this).trigger('onDel');
                this.win = null
            }
        },
        addEvent: function(){
            var t = this;
            $('[event_type=close]', this.win).on('click', function(e){
                e.preventDefault()
                t.close()
                stopBubble(e);
                return false;
            });
            $('[event_type=del]', this.win).on('click', function(e){
                t.del();
                stopBubble(e);
                return false
            });
        }
    }
    return win
});


LJS.define('mod.cAlert',function(ljs){

    var _temp = ljs.mod.modTemplate;
	var _layer = ljs.mod.win;
    /*//文本，是否自动关闭(false为自动关闭)  按钮   关闭时间  
     * {txt
     *  caClose
     *  btnTxt
     *  time}
     */
    var Alert = function(op){
        
        var opt = {
		    tpl : '<div class="inforPop">\
						<div class="title"><span class="btnExit"><a href="javascript:void(0);" event_type="del"></a></span></div>\
						<div class="txtGod"><span>#{txt}</span></div>\
					</div>',
            caClose : true,
            time : 2000,
            txt : '',
            btnTxt : '确定'
        }
        
        
        $.extend(opt,op)
        opt.tpl = _temp(opt.tpl,{
    			txt : opt.txt,
                btnTxt : opt.btnTxt
    		});
        
	    var _l = new _layer({
			tpl:opt.tpl
		});
        if(!opt.caClose){
            setTimeout(function(){
			_l.win && $(_l.win).hide("fast",function(){
				try{
					if(_l.win){
                    _l.del()
               }
				}catch(e){}
			});
		    },opt.time);
        }
		_l.show();
		return _l
    }
	return Alert
});
LJS.define('mod.cPrompt',function(ljs){
	var _layer = ljs.mod.cAlert;
    var _temp = ljs.mod.modTemplate;
    var _l = null
	function stopBubble(e){
        if (e && e.stopPropagation) 
            e.stopPropagation()
        else 
            window.event.cancelBubble = true
    }
    return function(op){
        
         var opt = {
			tpl : '<div class="infor inforNew inforJoin">\
					<div class="title"><span class="txt">信息提示</span><span class="btnExit"><a href="javascript:void(0)" event_type="del"></a></span></div>\
					<div class="fillList">\
						#{txt}\
					</div>\
					<div class="btnEn" style="padding-left:75px; padding-top:30px;"><a href="javascript:void(0)" event_type="ok" class="btn-et"><span>确定</span></a><a href="javascript:void(0);" event_type="del"" class="btn-ex"><span>取消</span></a></div>\
				</div>',
            caClose : true,
            time : 2000,
            txt : '',
            btnTxt : '确定',
            fun : function(){}
        }
        
        
        $.extend(opt,op)
        opt.tpl = _temp(opt.tpl,{
    			txt : opt.txt,
                btnTxt : opt.btnTxt
    	});
        
		
		if(_l){return _l}
		
		
	    _l = _layer(opt)
		
		$('[event_type="ok"]',_l.win).on('click',function(e){
			if($.isFunction(opt.fun)){
			 opt.fun();
			}
			stopBubble(e);
            return false;
		})
		
		$(_l).on('onDel',function(){
			_l = null
		})
		
		return _l
    }
	
});

LJS.define('cpt.allSelect', function(ljs){
    var that = {};
    that.allSelect = function(){
        var eleArr = $('[form-type="check"]', that.listBox);
        $.each(eleArr, function(i, ele){
            if (!ele.checked) {
                ele.checked = true;
            }
        });
        
        $('[form-type="allSelect"]').attr('checked','checked')
    }
    that.allDelSelect = function(){
    	var eleArr = $('[form-type="check"]', that.listBox);
        $.each(eleArr, function(i, ele){
                ele.checked = false;
        });
        $('[form-type="allSelect"]').attr('checked',null)
    }
    that.sendRequest = function(data){
        data = data.join(',');
        $.ajax({
            url: that.ajaxURL||'test.txt',
            data: {
                ids: data
            },
			type:that.method,
            dataType: 'json',
            cache: false,
            success: function(json){
                that.layer.del();
				that.layer = null;
                if (json.status == '100000') {
                    ljs.mod.cAlert({
                        tpl: '<div class="inforPop"><div class="title"><span class="btnExit"><a href="javascript:void(0);"></a></span></div><div class="txtGod"><span>删除成功!</span></div></div>',
                        caClose: true,
                        btnTxt: '确定'
                    });
					var setT = window.setTimeout(function(){
						setT = null;
						window.location.reload();
					},2000);
                }else if(json.status=='100003'){
                	window.location.href = json.content.url;
                }
                else {
                    alert(json.msg);
                }
            },
            error: function(){
                that.layer.del();
				that.layer = null;
                alert('数据加载失败');
            }
        });
    }
    that.addEvent = function(){
        that.selectBtn.on('click', function(e){
        	if(that.selectBtn.attr('selecting')!='true'){
        		that.selectBtn.attr('selecting','true')
        		that.allSelect();
        	}else{
        		that.selectBtn.attr('selecting','false')
        		that.allDelSelect();
        		
        	}
            
            return false;
        });
        var select = $('[form-type="allSelect"]', that.listBox);
        select.on('click', function(e){
            if(that.selectBtn.attr('selecting')!='true'){
        		that.selectBtn.attr('selecting','true')
        		that.allSelect();
        	}else{
        		that.selectBtn.attr('selecting','false')
        		that.allDelSelect();
        		
        	}
        });
        $('[form-type="check"]', that.listBox).on('click',function(){
        	
        	if(!$(this).attr('checked')){
        		
        		that.selectBtn.attr('selecting','false')
        		$('[form-type="allSelect"]').attr('checked',null)
        	}
        	
        	
        })
        that.allDel.on('click', function(e){
            var eleArr = $('[form-type="check"]', that.listBox);
            var arr = [];
            $.each(eleArr, function(i, ele){
                if (ele.checked) {
                    ele.checked = true;
                    arr.push(ele.getAttribute('form-data'));
                }
            });
            if (arr.length == 0) {
                alert('请选择删除项');
                return false;
            }
            that.layer = ljs.mod.cPrompt({
                tpl: '<div class="infor"><div class="title"><span class="txt">信息提示</span><span class="btnExit"><a href="javascript:void(0);" event_type="del"></a></span></div><dl class="conTxt"><dt><span>您确定要删除这</span><span class="colorRem">' + arr.length + '</span><span>项吗？</span></dt>\
                <dd><span class="colorRem">删除后将不可恢复~</span></dd></dl><div class="btnEn"><a class="btn-et" href="javascript:void(0);" event_type="ok"><span>确定</span></a><a class="btn-ex" href="javascript:void(0);" event_type="del"><span>取消</span></a></div></div>',
                caClose: true,
                txt: '',
                btnTxt: '确定',
                fun: function(){
                    that.sendRequest(arr);
                }
            });
        });
        that.listBox.delegate('a[button-type="del"]', 'click', function(e){
            var e = e || window.event;
            target = e.target || e.srcElement;
            var arr = [target.getAttribute('data')];
            that.layer = ljs.mod.cPrompt({
                tpl: '<div class="infor"><div class="title"><span class="txt">信息提示</span><span class="btnExit"><a href="javascript:void(0);" event_type="del"></a></span></div><dl class="conTxt"><dt><span>您确定要删除该项吗？</span></dt>\
                <dd><span class="colorRem">删除后将不可恢复~</span></dd></dl><div class="btnEn"><a class="btn-et" href="javascript:void(0);" event_type="ok"><span>确定</span></a><a class="btn-ex" href="javascript:void(0);" event_type="del"><span>取消</span></a></div></div>',
                caClose: true,
                txt: '',
                btnTxt: '确定',
                fun: function(){
                    that.sendRequest(arr);
                }
            });
        });
    }
    that.init = function(url,method){
		that.method = method||'GET';
        that.ajaxURL = url;
        that.listBox = $('#list');
        that.allDel = $('#allDel');
        that.selectBtn = $('#allSelect');
		that.allDelSelect();
        that.addEvent();
    }
    return that;
});


LJS.define('page.comManage',function(ljs){
   //搜索部分初始化
   ljs.cpt.search.init();
   
   //全选、删除功能初始化
   ljs.cpt.allSelect.init('/interact/comdel');
});